package com.dsw.demo.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayRabbitMQConfig {
    public static final String DELAY_EXCHANGE = "delay_exchange";//消息交换机的名字
    public static final String DELAY_QUEUE = "delay.queue";//消息队列名称
    public static final String DELAY_ROUTING_KEY = "delay.routing.key";//消息队列routingkey


    /**
     * 配置延时消息交换机  CustomExchange:按照routingkey分发到指定队列
     *
     * @return org.springframework.amqp.core.CustomExchange
     * @author coder
     * @date 2019/7/9 2:55 PM
     */
    @Bean(name = "DelayExchange")
    public CustomExchange delayExchange() {

        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }


    /**
     * 配置消息队列
     *
     * @author keyu.wang
     * @date 2018/5/7 16:38
     */
    @Bean(name = "DelayQueue")
    public Queue queue() {
        return new Queue(DELAY_QUEUE, true); //队列持久

    }


    /**
     * 将消息队列与交换机绑定
     *
     * @param delayExchange 交换机
     * @param delayQueue    队列
     * @author keyu.wang
     * @date 2018/5/7 16:37
     */
    @Bean
    public Binding bindingQueueDelayExchange(@Qualifier("DelayExchange") CustomExchange delayExchange, @Qualifier("DelayQueue") Queue delayQueue) {
        Binding binding = BindingBuilder.bind(delayQueue).to(delayExchange).with(DelayRabbitMQConfig.DELAY_ROUTING_KEY).noargs();
        return binding;
    }
}
